package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description:
 * create by sxy 2021/6/7
 */
@Api(tags = "business模块-分类相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {
    /**
     * //加载父级分类数据
     *
     * @return
     */
    @Autowired
    ProductCategoryService productService;

    //类别列表展示
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        PageVO<ProductCategoryTreeNodeVO> categoryList = productService.categoryTree(pageNum, pageSize);
        return ResponseBean.success(categoryList);
    }

    //获取父级分类数据
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productService.getParentCategoryTree();
        return ResponseBean.success(productCategoryTreeNodeVOList);
    }

    //添加分类
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ProductCategoryVO productCategoryVO) throws SystemException {
        productService.add(productCategoryVO);
        return ResponseBean.success();

    }

    //修改分类之回显数据
    @GetMapping("edit/{id}")
    @ApiOperation(value = "编辑分类")
    @RequiresPermissions({"productCategory:edit"})
    public ResponseBean<ProductCategoryVO> edit(@PathVariable Long id) {
        ProductCategoryVO productCategoryVO = productService.edit(id);
        return ResponseBean.success(productCategoryVO);

    }

    //修改分类
    @PutMapping("update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody ProductCategory productCategory) throws SystemException {
        productService.update(id, productCategory);
        return ResponseBean.success();

    }

    //删除分类
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws SystemException {
        productService.delete(id);
        return ResponseBean.success();
    }
}
